package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// Robot 机器人信息
type Device struct {
	ID                    string   `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	RobotID               string   `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:robot_id,unique;comment:机器人id"`
	MacAddr               string   `json:"mac_addr" gorm:"column:mac_addr;type:string;size:40;default:'';comment:网卡地址"`
	MerchantID            string   `json:"merchant_id" gorm:"column:merchant_id;type:int;size:64;index:merchant_id;default:1;comment:所属商户"`
	MerchantPath          string   `json:"merchant_path" gorm:"column:merchant_path;type:string;size:200;index:merchant_path;default:null;comment:商户开通路径,如>1>2"`
	IdentificationCode    string   `json:"identification_code" gorm:"column:identification_code;type:string;size:40;index:identification_code,unique;default:null;comment:机器人唯一id"`
	DeviceType            int      `json:"device_type" gorm:"column:device_type;type:int;size:32;comment:设备类型"`
	DeviceIP              string   `json:"device_ip" gorm:"column:device_ip;type:string;size:40;comment:机器ip"`
	LocationID            string   `json:"location_id" gorm:"column:location_id;type:int;size:64;default:null;comment:机器人所在位置"`
	FloorID               string   `json:"floor_id" gorm:"column:floor_id;type:int;size:64;default:null;comment:楼层id"`        // 关联的是floor表
	PositionID            string   `json:"position_id" gorm:"column:position_id;type:string;size:64;default:null;comment:点位"` // 关联的是device_postion表
	CurrentMapName        string   `json:"current_map_name" gorm:"column:current_map_name;type:string;size:40;comment:当前正在使用的地图名称"`
	LatestActiveTime      int64    `json:"latest_active_time" gorm:"column:latest_active_time;type:int;size:64;default:0;comment:最后一次活跃时间"`
	TotalRunningOdometer  int64    `json:"total_running_odometer" gorm:"column:total_running_odometer;type:int;size:64;default:0;comment:总的里程"`
	TotalRunningDuration  int64    `json:"total_running_duration" gorm:"column:total_running_duration;type:int;size:64;default:0;comment:总的活跃时长"`
	TotalChargingDuration int64    `json:"total_charging_duration" gorm:"column:total_charging_duration;type:int;size:64;default:0;comment:总的充电时长"`
	TotalIdleDuration     int64    `json:"total_idle_duration" gorm:"column:total_idle_duration;type:int;size:64;default:0;comment:总的空闲时长"`
	TotalTaskOdometer     int64    `json:"total_task_odometer" gorm:"column:total_task_odometer;type:int;size:64;default:0;comment:总的任务里程"`
	TotalTaskDuration     int64    `json:"total_task_duration" gorm:"column:total_task_duration;type:int;size:64;default:0;comment:总的任务时长"`
	ServicePhone          string   `json:"service_phone" gorm:"column:service_phone;type:string;size:20;default:null;comment:服务电话"`
	FirmwareVersion       string   `json:"firmware_version" gorm:"column:firmware_version;type:string;size:40;default:null;comment:固件版本"`
	Battery               int8     `json:"battery" gorm:"column:battery;type:int;size:8;default:0;comment:电量"`
	CreatedAt             int64    `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;default:0;comment:创建时间"`
	UpdatedAt             int64    `json:"updated_at" gorm:"column:updated_at;type:int;size:64;default:0;comment:更新时间"`
	Images                []string `json:"images" gorm:"column:images;type:json;serializer:json;default:'[]';comment:图片"`
}

// TableName 表名
func (Device) TableName() string {
	return "device"
}

// BeforeCreate 自增id
func (d *Device) BeforeCreate(tx *gorm.DB) (err error) {
	if d.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	d.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Device{})
}
